package com.thundersoft.pickcolor.database;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 * 一般来说，ContentProvider是单实例模式，
 * 当多个应用程序通过ContentResolver来操作ContentProvider提供的数据时，
 * ContentResolver调用的数据将会委托给同一个ContentProvider处理
 */
public class ColorContentProvider extends ContentProvider {
    public static final int COLOR_DIR = 0;
    public static final int COLOR_ITEM = 1;
    public static final String AUTHORITY = "com.thundersoft.pickcolor.provider";
    private static UriMatcher uriMatcher;//对Uri参数进行判断
    private DatabaseHelper dbHelper;

    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY,"color",COLOR_DIR);// 向UriMatcher对象注册Uri
        uriMatcher.addURI(AUTHORITY,"color/#",COLOR_ITEM);
    }

    /**
     * 当其他应用通过ContentResolver第一次访问该ContentProvider时，onCreate()会被调用，
     * onCreate()方法只会被调用一次
     */
    @Override
    public boolean onCreate() {
        dbHelper = new DatabaseHelper(getContext(),"PickColor.db",null,1);
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        //查询数据
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = null;

        // match:根据前面注册的Uri来判断指定Uri对应的标识码，如果找不到匹配的标识码，该方法将返回-1。
        switch (uriMatcher.match(uri)){
            case COLOR_DIR:
                cursor = db.query("Color",projection,selection,selectionArgs,null,
                        null,sortOrder);
                break;
            case COLOR_ITEM:
                String colorId = uri.getPathSegments().get(1);
                cursor = db.query("Color",projection,"id=?",new String[]{colorId},
                        null,null,sortOrder);
                break;
            default:
                break;
        }
        return cursor;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // 添加数据
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Uri uriReturn = null;

        // match:根据前面注册的Uri来判断指定Uri对应的标识码，如果找不到匹配的标识码，该方法将返回-1。
        switch (uriMatcher.match(uri)){
            case COLOR_DIR:
            case COLOR_ITEM:
                long newColorId = db.insert("Color",null,values);
                uriReturn = Uri.parse("content://"+AUTHORITY+"/color/"+newColorId);

                // 通知数据已经发生改变
                getContext().getContentResolver().notifyChange(uriReturn, null);
                break;
            default:
                break;
        }
        return uriReturn;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // 更新数据
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int updatedRows = 0;

        // match:根据前面注册的Uri来判断指定Uri对应的标识码，如果找不到匹配的标识码，该方法将返回-1。
        switch (uriMatcher.match(uri)){
            case COLOR_DIR:
                updatedRows = db.update("Color",values,selection,selectionArgs);

                // 通知数据已经发生改变
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            case COLOR_ITEM:
                String colorId = uri.getPathSegments().get(1);
                updatedRows = db.update("Color",values,"id=?",new String[]{colorId});

                // 通知数据已经发生改变
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            default:
                break;
        }
        return updatedRows;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // 删除数据
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int deletedRows = 0;

        // match:根据前面注册的Uri来判断指定Uri对应的标识码，如果找不到匹配的标识码，该方法将返回-1。
        switch (uriMatcher.match(uri)){
            case COLOR_DIR:
                deletedRows = db.delete("Color",selection,selectionArgs);

                // 通知数据已经发生改变
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            case COLOR_ITEM:
                String colorId = uri.getPathSegments().get(1);
                deletedRows = db.delete("Color","id=?",new String[]{colorId});

                // 通知数据已经发生改变
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            default:
                break;
        }
        return deletedRows;
    }

    /**
     * 返回当前Uri所代表的数据的MIME类型
     */
    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)){
            case COLOR_DIR:
                return "vnd.android.cursor.dir/vnd.com.thundersoft.pickcolor.provider.color";
            case COLOR_ITEM:
                return "vnd.android.cursor.dir/vnd.com.thundersoft.pickcolor.provider.color";
        }
        return null;
    }

}
